package com.jcz.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.jcz.dao.HibernateUtil;
import com.jcz.dao.Vote_SubjectDao;
import com.jcz.entity.Vote_Option;
import com.jcz.entity.Vote_Subject;

public class Vote_SubjectDaoImpl implements Vote_SubjectDao {

	/*查找全部subject*/
	@Override
	public List<Vote_Subject> findAll(String keywords,Integer page,Integer rows) {
		Session session=null;
		List<Vote_Subject> list=null;
		
		try {
			session=HibernateUtil.currentSession();
			Criteria criteria=session.createCriteria(Vote_Subject.class);
			if(keywords!=null){
				criteria.add(Restrictions.ilike("vs_title", "%"+keywords+"%"));
			}
			criteria.setFirstResult((page-1)*rows);
			criteria.setMaxResults(rows);
			list=criteria.list();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	/*根据ID查找subject*/
	@Override
	public Vote_Subject findById(Integer id) {
		Session session=null;
		Vote_Subject subject=null;
		try {
			session=HibernateUtil.currentSession();
			subject=(Vote_Subject)session.get(Vote_Subject.class, id);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return subject;
	}
	/*添加*/
	@Override
	public boolean addSubject(Vote_Subject subject) {
		Session session=null;
		Transaction tx=null;
		
		try {
			session=HibernateUtil.currentSession();
			tx=session.beginTransaction();
			
			session.save(subject);
			
			tx.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			tx.rollback();
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/*删除*/
	@Override
	public boolean delSubject(Integer id) {
		Session session=null;
		Transaction tx=null;
		
		try {
			session=HibernateUtil.currentSession();
			tx=session.beginTransaction();
			
			Vote_Subject subject=(Vote_Subject)session.get(Vote_Subject.class, id);
			session.delete(subject);
			
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/*更新*/
	@Override
	public boolean updateSubject(Vote_Subject subject) {
		Session session=null;
		Transaction tx=null;
		
		try {
			session=HibernateUtil.currentSession();
			tx=session.beginTransaction();
			
			session.update(subject);
			
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public long findCount() {
		Session session=null;
		long count=0;
		
		try {
			session=HibernateUtil.currentSession();
			Criteria criteria=session.createCriteria(Vote_Subject.class);
			criteria.setProjection(Projections.count("vs_id"));
			count=(long)criteria.uniqueResult();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}	

}
